<?php


namespace app\api\model;


use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;

class Qrcode
{
    public static function createQrcode($content)
    {
        $qrCode = new \Endroid\QrCode\QrCode($content);
        $qrCode->setSize(300);

        // Set advanced options
        $qrCode->setWriterByName('png');
        $qrCode->setMargin(0);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        $qrCode->setLabel('', 16, VENDOR_PATH.'/endroid/qr-code/assets/fonts/noto_sans.otf', LabelAlignment::CENTER());
//        $qrCode->setLogoPath(VENDOR_PATH.'/endroid/qr-code/assets/images/symfony.png');
        $qrCode->setLogoSize(150, 200);
        $qrCode->setRoundBlockSize(false);
        $qrCode->setValidateResult(false);
        $qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

        // Directly output the QR code
        header('Content-Type: '.$qrCode->getContentType());
//        echo $qrCode->writeString();
        $dir = ROOT_PATH.'/public/uploads/qrcode/';
        if (!file_exists($dir)) {
            mkdir($dir, 0755, true);
        }
        $file = $dir . md5($content).'.png';
        // Save it to a file
        $qrCode->writeFile($file);

        $image = str_replace(ROOT_PATH.'/public', request()->domain(), $file);
        return $image;
    }
}